function Quiz() {
	this.chooseBook();
	this.downloadBook();
	this.initBook();
	this.ask();
}
Quiz.prototype.bookCatalogue = 
	[
	 {'url': '01.txt',
	  'title': 'elementary level',
	  'tab': 4,
	  'mode': [[1, 3], [3, 1]],
	  'modeLabel': ['English to Japanese', 
					'Japanese to English'],
	  'type': 2,
	  'typeLabel': {'a': 'Adjective',
					'ad': 'Adverb',
					'n': 'Noun',
					'v': 'Verb'}
	 },
	 {'url': '02.txt',
	  'title': 'highschool level',
	  'tab': 4,
	  'mode': [[1, 3], [3, 1]],
	  'modeLabel': ['English to Japanese', 
				'Japanese to English']
	  'type': 2,
	  'typeLabel': {'a': 'Adjective',
					'ad': 'Adverb',
					'n': 'Noun',
					'v': 'Verb'}
	 },
	 {'url': '03.txt',
	  'title': 'college level',
	  'tab': 4,
	  'mode': [[1, 3], [3, 1]],
	  'modeLabel': ['English to Japanese', 
				'Japanese to English']
	  'type': 2,
	  'typeLabel': {'a': 'Adjective',
					'ad': 'Adverb',
					'n': 'Noun',
					'v': 'Verb'}
	 },
	 {'url': '04.txt',
	  'title': 'graduate level',
	  'tab': 4,
	  'mode': [[1, 3], [3, 1]],
	  'modeLabel': ['English to Japanese', 
				'Japanese to English']
	  'type': 2,
	  'typeLabel': {'a': 'Adjective',
					'ad': 'Adverb',
					'n': 'Noun',
					'v': 'Verb'}
	 }
	 ];
Quiz.prototype['chooseBook'] = function (num) {
	this.book = this.bookCatalogue[num];
	this.mode = 0;
    $("direction").value = this.book['modeLabel'][this.mode];
	return this;
};
Quiz.prototype['downloadBook'] = function () {
    $("loading").innerHTML = "now loading...";
    new Ajax.Request(filename, {
        method: 'get',
        onSuccess: function (transport) {
            this.response = transport.responseText;
            $("loading").innerHTML = "... has been loaded.";
		},
		onFailuer: function () {
            $("loading").innerHTML = "... couldn't be loaded.";
		}
    });
	return this;
};
Quiz.prototyoe['random'] = function (sup) {
	return Math.floor(Math.random() * sup);
};
Quiz.prototype['initBook'] = function () {
    var lines = response.split("\r\n");
	this.table = [];
	var tablen = this.book['tab'];
	var j = 0;
    for (var i=0; i < lines.length; i++) {
        var item = lines[i].split("\t");
        if (item.length != tablen) {
            continue;
        }
        this.table[j] = item;
		j++;
    }
	var tablelen = this.table.length
	for (var i=0; i < tablelen; i++) {
		var j = this.random(tablelen);
		if (i == j) {
			continue;
		}
		var tmp = this.table[j];
		this.table[j] = this.table[i];
		this.table[j] = tmp;
	}
	return this;
};
Quiz.prototype['nextQuestion'] = 0;
Quiz.prototype['ask'] = function(num) {
	this.correctAnswer = this.nextQuestion++;
	this.candidates[0] = this.correctAnswer;
	var i = 1;
	while (i < num) {
		var r = this.random(this.word.length);
		var dup = false;
		for (var j=0; j<i; j++) {
			if (this.candidates[j] == r) {
				dup = true;
				break;
			}
		}
		if (dup) {
			continue;
		}
		i++;
	}
	return this;
};
Quiz.prototype['answer'] = function(num) {
};

function setCandidates(num) {
    var i = 0;
    while (i < 3) {
        var dup = false;
        candidates[i] = catalogue[num][getRandom(catalogue[num].length)];
        for (var j=0; j<i; j++) {
            if (candidates[i] == candidates[j]) {
                dup = true;
                break;
            }
        }
        if (!dup) {
			// $(answerID[i]).value = words[candidates[i]][answerLang] + 
			//	" " + candidates[i];
			$(answerID[i]).value = words[candidates[i]][answerLang];
            i++;
        }
    }
}

var candidates;
var correctAnswer;
var answered;
var correctID;
function initQA() {
    candidates = new Array();
	correctAnswer = null;
	answered = 1;
}

function nextQuestion() {
    initQA();
    var questionType = words[getRandom(2000)][1];
    setCandidates(questionType);
    correctAnswer = getRandom(3);
    setQuestion();
}

function setQuestion() {
    correctID = candidates[correctAnswer];
    var correctWord = words[correctID];
    $("question").innerHTML = correctWord[questionLang];
    $("type").innerHTML = getClassName(correctWord[1]);
}    

function pushedAnswer(answer) {
    if (correctAnswer != answer) {
        $(answerID[answer]).value = answerText[1];
		answered = 0;
        return;
    }
	renewScore(answered);
	displayScore();
    nextQuestion();
}

function getSelectedRadio(radios) {
    var resnum = 0;
    for (var i=0; i < radios.length; i++) {
        if (radios[i].cheched) {
            resnum = i;
            break;
	    }
	}
	return radios[resnum];
}

function switchLangsDirection() {
    langsDirection++;    
    langsDirection %= 2;    
    $("direction").value = directionTexts[langsDirection];
    loadTabbedData(getSelectedRadio(document.settings.level));
}
